package com.lanou.service.impl;

import com.lanou.entity.TbContentCategory;
import com.lanou.entity.TbContentCategoryExample;
import com.lanou.mapper.TbContentCategoryMapper;
import com.lanou.service.ContentCategoryService;
import com.lanou.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * Created by QuChao on 2018/11/23
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;


    @Override
    public List<Map<String, Object>> list() {
        List tree = getTree(0L);
        return tree;
    }

    /*
     * 添加类目
     * */
    @Override
    @Transactional
    public List<Map<String, Object>> addCategory(Long parentId, String name) {
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andNameEqualTo(name).andParentIdEqualTo(parentId);
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        if (list.size() == 0) {
            Date date = new Date();
            TbContentCategory tbContentCategory = new TbContentCategory();
            tbContentCategory.setId(parentId);
            tbContentCategory.setIsParent(true);
            tbContentCategory.setUpdated(date);
            int i = tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
            if (i == 1) {
                TbContentCategory tbContentCategory1 = new TbContentCategory();
                tbContentCategory1.setParentId(parentId);
                tbContentCategory1.setName(name);
                tbContentCategory1.setStatus(1);
                tbContentCategory1.setSortOrder(1);
                tbContentCategory1.setIsParent(false);
                tbContentCategory1.setCreated(date);
                tbContentCategory1.setUpdated(date);
                int insert = tbContentCategoryMapper.insert(tbContentCategory1);
                if (insert == 1) {
                    return getTree(0L);
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }


    @Override
    public boolean updateById(Long id, String name) {
        TbContentCategory contentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andNameEqualTo(name).andParentIdEqualTo(contentCategory.getParentId());
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        if (list.size() == 0) {
            TbContentCategory tbContentCategory = new TbContentCategory();
            tbContentCategory.setId(id);
            tbContentCategory.setName(name);
            int i = tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
            if (i == 1) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        //1. 通过id查找到对象
        //2. 判断这个对象是不是父类
        //3. 是父类，删除这个父类以及父类下的子类
        //4. 不是父类，判断父类下的子类数量，
        //5. 大于1，删除自己，小于等于1，更改父类的isParent属性，删除自己
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);

        deleteContentCategory(tbContentCategory);

    }

    private void deleteContentCategory(TbContentCategory tbContentCategory) {
        if (tbContentCategory.getIsParent()) {
            TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
            tbContentCategoryExample.createCriteria().andParentIdEqualTo(tbContentCategory.getId());
            List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
            for (TbContentCategory contentCategory : list) {
                deleteContentCategory(contentCategory);
            }
            tbContentCategoryMapper.deleteByPrimaryKey(tbContentCategory.getId());
        } else {
            TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
            tbContentCategoryExample.createCriteria().andParentIdEqualTo(tbContentCategory.getParentId());
            List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
            if (list.size() <= 1) {
                TbContentCategory tbContentCategory1 = new TbContentCategory();
                tbContentCategory1.setId(tbContentCategory.getId());
                tbContentCategory1.setIsParent(false);
                tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory1);
            }
            tbContentCategoryMapper.deleteByPrimaryKey(tbContentCategory.getId());
        }
    }


    private List getTree(Long parentId) {
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andParentIdEqualTo(parentId);
        List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbContentCategory tbContentCategory : tbContentCategories) {
            Map<String, Object> map = new HashMap<>();
            if (tbContentCategory.getIsParent()) {
                map.put("id", tbContentCategory.getId());
                map.put("target", true);
                map.put("text", tbContentCategory.getName());
                List tree = getTree(tbContentCategory.getId());
                map.put("children", tree);
            } else {
                map.put("id", tbContentCategory.getId());
                map.put("target", false);
                map.put("text", tbContentCategory.getName());
            }
            list.add(map);
        }
        return list;
    }
}
